<?php
/**
 * Created by PhpStorm.
 * User: JSQ-SL
 * Date: 2018/11/19
 * Time: 15:37
 */

namespace app\index\table;

use app\index\controller\Login;
use think\Db;
use app\index\service\ExcelService;

class StockTable extends BaseTable
{
    protected static $view = 'V_ASSET_STOCK';
    protected static $table = 'ASSET_STOCK';
    protected static $pk = 'STOCK_NO';
    protected static $configTable = 'ASSET_STOCK_CONFIG';

    static public function table(){
        return Db::table(static::$view);
    }

    static public function add($data){
        $data[self::$pk] = static::getNextId();
        Db::table(static::$table)->insert($data);
    }

    static public function addImport($data){
        Db::table(static::$table)->insert($data);
    }

    static public function update($where,$data){
        Db::table(static::$table)->where($where)->update($data);
    }

    static public function delete($where){
        return Db::table(static::$table)->where($where)->delete();
    }

    public static function getConfigAdd(){
        return Db::table(static::$configTable)->where(['ADD_FLAG'=>'1'])->order('ID ASC')->select();
    }

    public static function getConfigEdit(){
        return Db::table(static::$configTable)->where(['EDIT_FLAG'=>'1'])->order('ID ASC')->select();
    }

    public static function getConfigPrint(){
        return Db::table(static::$configTable)->where(['PRINT_FLAG'=>'1'])->order('ID ASC')->select();
    }

    public static function getNextId(){
        $year = date('Y');
        $numMaxRecord = Db::table(static::$table)
            ->field('max(SUBSTR('.static::$pk.',5,4)) as NUM')
            ->where([['SUBSTR('.static::$pk.',1,4)','=',$year]])
            ->fetchSql(true)
            ->find();
        $numMaxRecord = Db::query($numMaxRecord);
        $num = $numMaxRecord[0]['NUM'];
        if ($num == NULL) {
            $num = 0;
        }
        $num++;
        return $year.sprintf("%04d", $num);
    }

    public static function exportExcel($data,$fileName){

        $fieldList = [
            ['title'=>'库存编号','field'=>'STOCK_NO'],
            ['title'=>'存货分类','field'=>'TYPE_NAME'],
            ['title'=>'品牌','field'=>'BRAND'],
            ['title'=>'规格型号','field'=>'SPEC'],
            ['title'=>'库存数量','field'=>'STOCK_NUM'],
            ['title'=>'单价','field'=>'PRICE'],
            ['title'=>'库存价值（总价）','field'=>'TOTAL_PRICE'],
            ['title'=>'供货商','field'=>'SUPPLIER'],
            ['title'=>'取得方式','field'=>'GET_TYPE'],
            ['title'=>'存放地点','field'=>'PLACE'],
        ];

        $excel = new ExcelService();
        $excel->exportExcel($data,$fileName,$fieldList);
    }

    const STOCK_FIELD_LIST = [
        "STOCK_NO","STOCK_TYPE_ID","BRAND","SPEC","STOCK_NUM","PRICE","TOTAL_PRICE","SUPPLIER","GET_TYPE_ID","PLACE"
        ];

    public static function getSessionConditions() {
        $condition = null;
        $listCondition = Login::getListConditionStock();
        if (!empty($listCondition['STOCK_NO'])) {
            $value = $listCondition['STOCK_NO'];
            $condition[] = ['STOCK_NO','like',"$value%"];
        }
        if (!empty($listCondition['STOCK_TYPE_ID'])) {
            $value = $listCondition['STOCK_TYPE_ID'];
            $condition[] = ['STOCK_TYPE_ID','=',$value];
        }
        if (!empty($listCondition['GET_TYPE_ID'])) {
            $value = $listCondition['GET_TYPE_ID'];
            $condition[] = ['GET_TYPE_ID','=',$value];
        }

        if (!empty($listCondition['STOCK_NUM_START'])) {
            $value = $listCondition['STOCK_NUM_START'];
            $condition[] = ['STOCK_NUM','>=',$value];
        }
        if (!empty($listCondition['STOCK_NUM_END'])) {
            $value = $listCondition['STOCK_NUM_END'];
            $condition[] = ['STOCK_NUM','<=',$value];
        }

        if (!empty($listCondition['PRICE_START'])) {
            $value = $listCondition['PRICE_START'];
            $condition[] = ['PRICE','>=',$value];
        }
        if (!empty($listCondition['PRICE_END'])) {
            $value = $listCondition['PRICE_END'];
            $condition[] = ['PRICE','<=',$value];
        }

        if (!empty($listCondition['TOTAL_PRICE_START'])) {
            $value = $listCondition['TOTAL_PRICE_START'];
            $condition[] = ['TOTAL_PRICE','>=',$value];
        }
        if (!empty($listCondition['TOTAL_PRICE_END'])) {
            $value = $listCondition['TOTAL_PRICE_END'];
            $condition[] = ['TOTAL_PRICE','<=',$value];
        }

        return $condition;

    }
}